/**
 * 
 */

import java.util.Iterator;
import java.util.LinkedList;

public class GraphConversion {

	/**
	 * @param The simple graph we'd run in our algorithm
	 * Initial the EdgeData, and put them to data variable
	 * @throws Exception 
	 */
	public static void ConvertGraph(SimpleGraph G) throws Exception
	{
		Iterator edges =  G.edges();
		//initial every edges;
		while(edges.hasNext())
		{
			Edge edge = (Edge)edges.next();
			Double capacity = (Double) edge.getData();
			EdgeData data = new EdgeData(capacity.intValue(),0);
			edge.setData(data);
		}
		//initial every vertex
		Iterator vertices =  G.vertices();

		return;
	}
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		SimpleGraph G = new SimpleGraph();
		GraphInput.LoadSimpleGraph(G, "/Users/stflaptop/Documents/workspace/FlowNetwork/random.txt");
//		ConvertGraph(G);
		ScalingFF sff = new ScalingFF();		
		Vertex s =null,t = null;
		Edge e;
		EdgeData edata;
		Iterator vertexs = G.vertices();
		Iterator i;
//get the source point and sink point
		for (i= G.vertices(); i.hasNext(); ) {
            Vertex v = (Vertex) i.next();
            if (v.getName().equals("s")) s=v;
            else if (v.getName().equals("t")) t=v;
            }
		
		if(s != null)
			System.out.println("s: " + s.getName());
		if(t != null)
			System.out.println("t: " + t.getName());

		int maxflow= sff.maxFlow(G, s, t);
		System.out.println(maxflow);
//		for (i = G.edges(); i.hasNext(); ){
//			e = (Edge) i.next();
//			edata = (EdgeData) e.getData();
//			System.out.println("Edge: " + e.getName() + " " 
//					+ e.getFirstEndpoint().getName() + "-" 
//					+ e.getSecondEndpoint().getName() + " "
//					+ edata.getFlow() + "/" + edata.getCapacity());
//		}		
		
		
		
		while(vertexs.hasNext())
		{
			Vertex vertex = (Vertex)vertexs.next();
			Iterator incidentEdges = vertex.incidentEdgeList.iterator();
			while(incidentEdges.hasNext())
			{
				Edge edge = (Edge)incidentEdges.next();
				System.out.print(vertex.getName());
				if(edge.getFirstEndpoint().getName().equals(vertex.getName()))
				{
					System.out.print(" incident edges: flow ");
					System.out.print(((EdgeData)edge.getData()).getFlow());
					System.out.print(" capacity ");
					System.out.print(((EdgeData)edge.getData()).getCapacity());
					}
				else
				{
					System.out.print(" The vertex is the end point of this incident edge.");
				}
				System.out.print("\n");
			}
		}
		
	}

}
